#!/bin/sh
# PCP QA Test No. 895
# pmlogger SIGINT badness
#
# https://github.com/performancecopilot/pcp/issues/116
#
# Copyright (c) 2016 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=1	# failure is the default!
trap "cd $here; $sudo rm -rf $tmp $tmp.*; exit \$status" 0 1 2 3 15

cat <<End-of-File >$tmp.config
log mandatory on 100 msec {
    containers
    disk
    event
    filesys
    hinv
    ipc
    kernel
    mmv
    pmcd
    pmda
    quota
    rpc
    swap
    swapdev
    sysfs
    tmpfs
    vfs
}
End-of-File

# some of these metrics are not available on some platforms ...
#
case $PCP_PLATFORM
in
    linux)
	;;

    darwin)
	sed <$tmp.config >$tmp.tmp \
	    -e '/ containers$/d' \
	    -e '/ quota$/d' \
	    -e '/ ipc$/d' \
	    -e '/ swap$/d' \
	    -e '/ swapdev$/d' \
	    -e '/ sysfs$/d' \
	    -e '/ tmpfs$/d' \
	    -e '/ vfs$/d' \
	# end
	mv $tmp.tmp $tmp.config
	;;

    freebsd)
	sed <$tmp.config >$tmp.tmp \
	    -e '/ containers$/d' \
	    -e '/ filesys$/d' \
	    -e '/ quota$/d' \
	    -e '/ ipc$/d' \
	    -e '/ rpc$/d' \
	    -e '/ swapdev$/d' \
	    -e '/ sysfs$/d' \
	    -e '/ tmpfs$/d' \
	    -e '/ vfs$/d' \
	# end
	mv $tmp.tmp $tmp.config
	;;

    openbsd)
	sed <$tmp.config >$tmp.tmp \
	    -e '/ ipc$/d' \
	    -e '/ quota$/d' \
	    -e '/ rpc$/d' \
	    -e '/ swapdev$/d' \
	    -e '/ sysfs$/d' \
	    -e '/ tmpfs$/d' \
	    -e '/ vfs$/d' \
	# end
	mv $tmp.tmp $tmp.config
	;;

    *)
	echo "Arrgh ... need test customization for $PCP_PLATFORM"
	exit
	;;
esac

_filter()
{
    cat \
    | tee -a $seq_full \
    | sed \
	-e '/re-established connection/d' \
	-e '/pduread: .* Interrupted system call/d' \
	-e '/Validating metrics/d' \
	-e '/^Fetch task/d' \
	-e '/^  Fetch group/d' \
	-e '/^Log for pmlogger /s/ on .*/ .../' \
	-e '/^preprocessor cmd:/s/: .*/: .../' \
	-e '/^Starting logger/s/".*/.../' \
	-e '/} logged every/s/: .*/: .../' \
	-e "s@$tmp@TMP@" \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/DATE/' \
	-e 's/DATE [0-9][0-9][0-9][0-9]/DATE/' \
	-e 's/pmlogger([0-9][0-9]*)/pmlogger(PID)/' \
    | $PCP_AWK_PROG '
BEGIN			{ state = 0 }
$1 == "Group"		{ print "Group ... {"; state = 1; next }
state == 1 && $1 == "}"	{ print "	..."; state = 0 }
state == 0		{ print }'
}

# real QA test starts here
export PCP_DERIVED_CONFIG=
pmlogger -Doptfetch -h localhost -r -c $tmp.config -m qa_$seq -l $tmp.log $tmp &
pid=$!

pmsleep 6
kill -INT $pid
wait

_filter <$tmp.log

N=`grep '^  Fetch group' $tmp.log | tee -a $seq_full | wc -l | sed -e 's/ //g'`

# pmlogger ends up using N fetch groups 
# without timeouts, expect 6000msec * N fetch groups / 100msec = 60*N records
# +2 for preamble and postscript records
#
# So the upper bound is 61*N records.
#
# On really slow VMs we struggle to keep up with the number of metrics and
# the 100msec sampling rate, so 30*N is the lower bound ... this is OK,
# because if pmlogger is going to behave badly we'll miss the 60*N target
# by a mile!
#
max_nrec=`expr $N \* 61 + 2`
min_nrec=`expr \( $N \) \* 30 + 2`
echo "N(fetch groups)=$N max_nrec=$max_nrec min_nrec=$min_nrec" >>$seq_full
nrec=`pmdumplog $tmp | grep '^[0-9]' | grep -v '<mark>' | wc -l | sed -e 's/ //g'`
echo "nrec=$nrec log records" >>$seq_full
echo "log record frequency by fetch group size ..." >>$seq_full
pmdumplog $tmp \
| grep '^[0-9]' \
| grep -v '<mark>' \
| sed -e 's/^[^ ][^ ]* //' \
| sort \
| uniq -c \
| sed -e 's/\([0-9]\) \([0-9]\)/\1 x \2/' >>$seq_full

if [ "$nrec" -ge $min_nrec -a "$nrec" -le $max_nrec ]
then
    echo "OK, found expected number of archive records"
else
    echo "oops, $nrec archive records not between the expected range of $min_nrec and $max_nrec"
    pmdumplog $tmp | grep '^[0-9]' | grep -v '<mark>' >>$seq_full
fi

# success, all done
status=0

exit
